package com.sinoglobal.ningxia.activity.news;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.view.listener.AbOnListViewListener;
import com.ab.view.pullview.AbPullListView;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.activity.vip.Login;
import com.sinoglobal.ningxia.adapter.NoImgCommentAdapter;
import com.sinoglobal.ningxia.beans.CommentIdVo;
import com.sinoglobal.ningxia.beans.CommentListVo;
import com.sinoglobal.ningxia.beans.CommentSubmitVo;
import com.sinoglobal.ningxia.beans.NewsBean;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.utils.EquipmentUtil;
import com.sinoglobal.ningxia.utils.FlyUtil;
import com.sinoglobal.ningxia.utils.TextUtil;

import java.text.MessageFormat;

/**
 * @author zhourihu
 * @createdate 2014年7月12日 上午9:16:06
 * @Description: TODO(用一句话描述该类做什么) 新闻评论 105 获取评论
 */
public class NewsCommentActivity extends BaseActivity implements
        AbOnListViewListener, OnClickListener {
    private AbPullListView commentList;
    EditText etComment;
    ImageView ivSendComment;
    NoImgCommentAdapter adapter;
    ProgressDialog progressDialog;
    NewsBean newsBean;
    byte type = 1, pageNo, pageSize = 8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.refresh_list);
        openChenJInShi();//开启沉浸式
        newsBean = (NewsBean) getIntent().getSerializableExtra(
                NewsBean.class.getCanonicalName());
        initView();
        addListener();
        loadData(true);
    }

    private void initView() {
        titleView.setText(getString(R.string.news_comment));
        etComment = (EditText) findViewById(R.id.comment_content);
        ivSendComment = (ImageView) findViewById(R.id.comment_submit);
        progressDialog = new ProgressDialog(this);
        commentList = (AbPullListView) findViewById(R.id.refreshList);
        commentList.getHeaderView().setHeaderProgressBarDrawable(
                this.getResources().getDrawable(R.drawable.progress_circular));
        commentList.getFooterView().setFooterProgressBarDrawable(
                this.getResources().getDrawable(R.drawable.progress_circular));
        commentList.setAbOnListViewListener(this);
        TextView tvEmpty = (TextView) findViewById(R.id.noData);
        tvEmpty.setVisibility(View.VISIBLE);
        commentList.setEmptyView(tvEmpty);
        adapter = new NoImgCommentAdapter(this, null);
        commentList.setAdapter(adapter);
    }

    private void addListener() {
        ivSendComment.setOnClickListener(this);
    }

    private void loadData(final boolean isShowDialog) {
        if (!EquipmentUtil.checkNetState(this)) {
            showShortToastMessage(getString(R.string.no_network));
            commentList.setPullLoadEnable(false);
            commentList.setPullRefreshEnable(false);
            return;
        }
        new ItktAsyncTaskWithDialog<Void, Void, CommentListVo>(
                getString(R.string.loading_news), isShowDialog, true) {

            @Override
            public CommentListVo before(Void... params) throws Exception {
                // Auto-generated method stub
                return RemoteImpl.getInstance().getCommentList(
                        newsBean.getId(), type, pageNo, pageSize);
            }

            @Override
            public void after(CommentListVo result) {
                commentList.stopLoadMore();
                commentList.stopRefresh();
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                setView(result);

            }

            @Override
            public void exception() {
                // Auto-generated method stub

            }

        }.execute();

    }

    private void setView(CommentListVo result) {
        if (result != null) {
            if (Code.CODE_SUCCESS != result.getCode()) {
                commentList.setPullLoadEnable(false);
                return;
            }
            if (pageNo == 0) {
                adapter.clearData();
            }
            adapter.addData(result.getList());
            if (result.getList() == null || result.getList().isEmpty()) {
                commentList.setPullLoadEnable(false);
            } else {
                commentList.setPullLoadEnable(true);
            }
        }
    }

    @Override
    public void onRefresh() {
        pageNo = 0;
        loadData(false);
    }

    @Override
    public void onLoadMore() {
        pageNo++;
        loadData(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.comment_submit:// 评论
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(etComment.getWindowToken(), 0); // 强制隐藏键盘
                if (TextUtil.stringIsNull(etComment.getText().toString())) {
                    showShortToastMessage(getString(R.string.no_content_comment));
                    return;
                }
                if (TextUtil.stringIsNull(FlyApplication.userId)) {
                    FlyUtil.intentForward(this, Login.class);
                    return;
                }
                if (etComment.getText().toString().length() > 50) {
                    String info = getString(R.string.commentInfo);
                    info = MessageFormat.format(info, etComment.length());
                    showShortToastMessage(info);
                    return;
                }
                sendComment(etComment.getText().toString());
                break;

            default:
                break;
        }

    }

    private void sendComment(final String comment) {
        ivSendComment.setEnabled(false);
        new AsyncTask<Void, Void, CommentIdVo>() {
            protected void onPreExecute() {
            }

            ;

            @Override
            protected CommentIdVo doInBackground(Void... params) {
                // Auto-generated method stub
                try {
                    return RemoteImpl.getInstance().submitComment(
                            new CommentSubmitVo(0, comment, newsBean.getId(),
                                    "1"), NewsCommentActivity.this);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            protected void onPostExecute(CommentIdVo result) {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                if (result != null) {
                    if (Code.CODE_SUCCESS == result.getCode()) {
                        showShortToastMessage(getResources().getString(
                                R.string.commentSuccess));
                        onRefresh();
                        etComment.setText(null);
                    } else {
                        showShortToastMessage(result.getMessage());
                        etComment.setText(null);
                    }
                    ivSendComment.setEnabled(true);
                }
            }

            ;
        }.execute();

    }
}
